package com.itcast.mallapplet.service.impl;

import com.itcast.applet.dto.ShopEvaluateDTO;
import com.itcast.applet.entity.Order;
import com.itcast.applet.entity.OrderDity;
import com.itcast.applet.entity.RiderEvaluate;
import com.itcast.context.BaseContext;
import com.itcast.mallapplet.mapper.OrderMapper;
import com.itcast.mallapplet.mapper.ShopEvaluateMapper;
import com.itcast.mallapplet.service.ShopEvaluateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ShopEvaluateServiceImpl implements ShopEvaluateService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ShopEvaluateMapper shopEvaluateMapper;

    //新增商品评价
    @Transactional
    public void shopEvaluateAdd(ShopEvaluateDTO shopEvaluateDTO) {
        Order order = orderMapper.orderGetById(shopEvaluateDTO.getOrderId());
        //拿到订单分表所有数据
        List<OrderDity> orderDities = orderMapper.orderDityAll(order.getId());
        for (OrderDity orderDity : orderDities) {
            RiderEvaluate riderEvaluate = RiderEvaluate.builder()
                    .shopId(orderDity.getShopId())
                    .evaluate(shopEvaluateDTO.getEvaluate())
                    .userId(BaseContext.getCurrentId())
                    .createTime(LocalDateTime.now())
                    .build();
            //插入评论数据
            shopEvaluateMapper.shopEvaluateAdd(riderEvaluate);
        }
        order.setStatus(5);
        orderMapper.amendStatus(order);
    }
}
